package com.akoj.anmu.controller;

import com.akoj.anmu.common.BaseResponse;
import com.akoj.anmu.common.ResponseCode;
import com.akoj.anmu.common.ResultUtils;
import com.akoj.anmu.exception.BusinessException;
import com.akoj.model.domain.dto.user.*;
import com.akoj.model.domain.vo.LoginUserVO;
import com.akoj.model.domain.vo.UserVO;
import com.akoj.anmu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.akoj.anmu.constant.UserConstant.ADMIN_ROLE;
import static com.akoj.anmu.constant.UserConstant.USER_STORE;

/**
 * 用户接口
 *
 * @author  akme
 */
@RestController("/user")
@Api(tags = "用户模块")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest 注册请求体
     * @return 用户注册ID
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        return ResultUtils.success(userService.UserRegister(userRegisterRequest));
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 登录请求体
     * @param request          请求信息
     * @return 脱敏后的登陆用户信息
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        return ResultUtils.success(userService.doLogin(userAccount, userPassword, request));

    }

    /**
     * 根据昵称查询用户信息
     *
     * @param userQueryRequest 查询用户请求体
     * @return 用户信息列表
     */
    @PostMapping("/searchBynickName")
    @ApiOperation(value = "根据昵称查询用户")
    public BaseResponse<List<UserVO>> searchUsersByNickName(@RequestBody UserQueryByNickNameRequest userQueryRequest, HttpServletRequest request) {
        //请求参数是否合法
        String nickName = userQueryRequest.getNickName();
        if ("".equals(nickName) || nickName == null) {
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        //判断是否登录以及权限是否满足
        userService.getCurrentUser(request);
        //执行业务逻辑查询
        return ResultUtils.success(userService.searchUsersByNickName(nickName));
    }

    /**
     * 根据ID删除用户信息
     *
     * @param userDeleteRequest    删除用户请求体
     * @param request 请求
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    public BaseResponse<Boolean> deleteUserById(@RequestBody UserDeleteRequest userDeleteRequest, HttpServletRequest request) {
        //判断ID是否为有效值
        Long id = userDeleteRequest.getId();
        if (id <= 0) {
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"用户ID错误");
        }
        //判断是否登录以及权限是否满足
        if(!userService.isAdmin(request)){
            throw new BusinessException(ResponseCode.NO_AUTH,"未登录或权限不足");
        }
        //执行业务逻辑删除
        return ResultUtils.success(userService.deleteUserById(id));
    }

    /**
     * 用户登出
     * @param request 请求
     * @return 等出结果
     */
    @PostMapping("/logout")
    @ApiOperation(value = "用户登出")
    public BaseResponse<Boolean> doLogout(HttpServletRequest request){
        Boolean logout = userService.doLogout(request);
        if(!logout){
            throw new BusinessException(ResponseCode.ERROR,"登出失败");
        }
        return ResultUtils.success(logout);
    }

    /**
     * 根据标签查询用户
     * @param userQueryRequest 查询请求体
     * @param request 请求
     * @return 用户信息列表
     */
    @PostMapping("/searchByTags")
    @ApiOperation(value = "根据标签查询用户")
    public BaseResponse<List<UserVO>> searchUsersByTags(@RequestBody UserQueryByTagsRequest userQueryRequest, HttpServletRequest request) {
        //请求参数是否合法
        List<String> tags = userQueryRequest.getTags();
        if (tags == null || tags.isEmpty()) {
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        LoginUserVO currentUser = userService.getCurrentUser(request);
        //执行业务逻辑查询
        return ResultUtils.success(userService.searchUsersByTags(tags,currentUser));
    }

    /**
     * 获取当前登录用户
     * @param request 请求
     * @return  登录用户信息
     */
    @PostMapping("/currentUser")
    @ApiOperation(value = "获取当前登录用户")
    public BaseResponse<LoginUserVO> getCurrentUser(HttpServletRequest request){
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(currentUser);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新用户信息")
    public BaseResponse<UserVO> updateUser(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request){
        //判断参数是否合法
        if( userUpdateRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"参数为空");
        }
        //判断是否是管理员以及有没有登陆
        LoginUserVO loginUser = userService.getCurrentUser(request);
        if(!userService.isAdmin(request) && loginUser == null){
            throw new BusinessException(ResponseCode.NO_AUTH, "未登录或权限不足");
        }
        //调用业务逻辑更新
        return ResultUtils.success(userService.updateUser(userUpdateRequest,loginUser));

    }

    @PostMapping("/recommend")
    @ApiOperation("推荐用户")
    public BaseResponse<List<UserVO>> searchRecommendUsers(HttpServletRequest request, @RequestParam("pageNum") Long pageNum, @RequestParam("pageSize") Long pageSize) {
        if(pageNum == null || pageSize == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"参数为空");
        }
        //执行业务逻辑查询
        return ResultUtils.success(userService.searchRecommendUsers(request, pageNum, pageSize));
    }

    @PostMapping("/matchUsers")
    @ApiOperation(value = "匹配用户")
    public BaseResponse<List<UserVO>> matchUsers(HttpServletRequest request) {
        LoginUserVO currentUser = userService.getCurrentUser(request);
        //执行业务逻辑查询
        return ResultUtils.success(userService.matchUsers(currentUser));
    }

}
